{% extends 'base.html' %}

{% block title %}为角色 "{{ role.name }}" 分配权限 - Django RBAC 项目{% endblock %}

{% block content %}
<div class="min-h-screen bg-gray-50 py-6">
    <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        {% if messages %}
        <div class="mb-6">
            {% for message in messages %}
            <div class="rounded-md {% if message.tags == 'error' %}bg-red-50{% else %}bg-green-50{% endif %} p-4">
                <div class="flex">
                    <div class="flex-shrink-0">
                        {% if message.tags == 'error' %}
                        <svg class="h-5 w-5 text-red-400" viewBox="0 0 20 20" fill="currentColor">
                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/>
                        </svg>
                        {% else %}
                        <svg class="h-5 w-5 text-green-400" viewBox="0 0 20 20" fill="currentColor">
                            <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
                        </svg>
                        {% endif %}
                    </div>
                    <div class="ml-3">
                        <p class="text-sm font-medium {% if message.tags == 'error' %}text-red-800{% else %}text-green-800{% endif %}">
                            {{ message }}
                        </p>
                    </div>
                </div>
            </div>
            {% endfor %}
        </div>
        {% endif %}

        <div class="mb-6">
            <h2 class="text-2xl font-bold text-gray-900">为角色 "{{ role.name }}" 分配权限</h2>
            <p class="mt-1 text-sm text-gray-500">选择要分配给该角色的权限</p>
        </div>

        <div class="bg-white shadow rounded-lg">
            <form method="post" class="divide-y divide-gray-200">
                {% csrf_token %}
                <div class="px-4 py-5 sm:p-6">
                    {% for group_name, permissions in permission_groups.items %}
                    {% if permissions %}
                    <div class="mb-8 last:mb-0">
                        <div class="flex items-center justify-between mb-4">
                            <h3 class="text-lg font-medium text-gray-900">{{ group_name }}</h3>
                            <button type="button"
                                    class="text-sm text-blue-600 hover:text-blue-800"
                                    onclick="toggleGroupPermissions('{{ group_name|slugify }}')">
                                全选/取消
                            </button>
                        </div>
                        <div class="grid grid-cols-1 gap-4 sm:grid-cols-2 lg:grid-cols-3" id="{{ group_name|slugify }}-group">
                            {% for permission in permissions %}
                            <div class="relative flex items-start">
                                <div class="flex items-center h-5">
                                    <input type="checkbox"
                                           name="permissions"
                                           value="{{ permission.id }}"
                                           id="permission_{{ permission.id }}"
                                           {% if permission.id in current_permissions %}checked{% endif %}
                                           class="permission-checkbox focus:ring-blue-500 h-4 w-4 text-blue-600 border-gray-300 rounded">
                                </div>
                                <div class="ml-3 text-sm">
                                    <label for="permission_{{ permission.id }}" class="font-medium text-gray-700">
                                        {{ permission.name }}
                                    </label>
                                    {% if permission.description %}
                                    <p class="text-gray-500">{{ permission.description }}</p>
                                    {% endif %}
                                </div>
                            </div>
                            {% endfor %}
                        </div>
                    </div>
                    {% endif %}
                    {% endfor %}
                </div>
                <div class="px-4 py-4 sm:px-6 bg-gray-50 flex justify-between">
                    <div class="flex items-center">
                        <button type="button"
                                onclick="toggleAllPermissions()"
                                class="inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                            全选/取消全部
                        </button>
                    </div>
                    <div class="flex space-x-3">
                        <a href="{% url 'manage_roles' %}"
                           class="inline-flex items-center px-4 py-2 border border-gray-300 shadow-sm text-sm font-medium rounded-md text-gray-700 bg-white hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                            取消
                        </a>
                        <button type="submit"
                                class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500">
                            保存更改
                        </button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
function toggleGroupPermissions(groupId) {
    const group = document.getElementById(`${groupId}-group`);
    const checkboxes = group.querySelectorAll('.permission-checkbox');
    const anyUnchecked = Array.from(checkboxes).some(cb => !cb.checked);
    checkboxes.forEach(cb => cb.checked = anyUnchecked);
}

function toggleAllPermissions() {
    const checkboxes = document.querySelectorAll('.permission-checkbox');
    const anyUnchecked = Array.from(checkboxes).some(cb => !cb.checked);
    checkboxes.forEach(cb => cb.checked = anyUnchecked);
}
</script>
{% endblock %}

